package com.zip;

import org.apache.commons.io.IOUtils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/**
 * zip追加文件
 * */
public class ZipAddFile {

    //private final static String desktop = "G:";
    private final static String desktop = "C:/Users/domains90/Desktop";


    public static void main(String[] args) {
        new ZipAddFile().add(desktop+"/aa.zip",desktop+"/center_health_msg.sql");
    }

    public void add(String zipPath,String filePath){
        try(FileOutputStream fos = new FileOutputStream(zipPath);ZipOutputStream zos = new ZipOutputStream(fos);) {
            // 创建一个输出流以写入 ZIP 文件



            // 创建一个输入流以从要添加的文件中读取数据
            FileInputStream fis = new FileInputStream(filePath);

            // 将文件添加到 ZIP 存档中

            zos.putNextEntry(new ZipEntry("cc.sql"));


            //IOUtils.copy(fis, zos);
//            byte[] buffer = new byte[1024];
//            int len;
//            while ((len = fis.read(buffer)) > 0) {
//                zos.write(buffer, 0, len);
//            }

            System.out.println(filePath + " 已成功添加到 " + zipPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }




}
